package com.drinkdelivery.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
//import org.springframework.security.access.prepost.PreAuthorize;
import com.drinkdelivery.common.domain.export.ProductsExport;
import com.drinkdelivery.common.request.ProductsRequest;
import com.drinkdelivery.common.response.ProductsDetailsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.drinkdelivery.common.annotation.Log;
import com.drinkdelivery.common.core.controller.BaseController;
import com.drinkdelivery.common.core.domain.AjaxResult;
import com.drinkdelivery.common.enums.BusinessType;
import com.drinkdelivery.common.domain.Products;
import com.drinkdelivery.service.IProductsService;
import com.drinkdelivery.common.utils.poi.ExcelUtil;
import com.drinkdelivery.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author lzh
 * @date 2024-11-08
 */
@Api(tags = "商品Controller")
@RestController
@RequestMapping("/admin/products")
public class ProductsController extends BaseController
{
    @Autowired
    private IProductsService productsService;

    /**
     * 查询商品列表
     */
    @ApiOperation(value = "查询商品列表")
//@PreAuthorize("@ss.hasPermi('drinkdelivery:products:list')")
    @GetMapping("/list")
    public TableDataInfo list(Products products)
    {
        startPage();
        List<ProductsDetailsResponse> list = productsService.selectProductsList(products);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @ApiOperation(value = "导出商品列表")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:products:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Products products)
    {

        List<ProductsExport> list = productsService.selectProductsExport(products);
        ExcelUtil<ProductsExport> util = new ExcelUtil<ProductsExport>(ProductsExport.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @ApiOperation(value = "获取商品详细信息")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:products:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(productsService.selectProductsById(id));
    }

    /**
     * 新增商品
     */
    @ApiOperation(value = "新增商品")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:products:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductsRequest productsRequest)
    {
        return toAjax(productsService.insertProducts(productsRequest));
    }

    /**
     * 修改商品
     */
    @ApiOperation(value = "修改商品")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:products:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductsRequest productsRequest)
    {
        return toAjax(productsService.updateProducts(productsRequest));
    }

    /**
     * 删除商品
     */
    @ApiOperation(value = "删除商品")
    //@PreAuthorize("@ss.hasPermi('drinkdelivery:products:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(productsService.deleteProductsByIds(ids));
    }
}
